<?php
namespace app\validate;

use think\Validate;

class SampleValidator extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'username' => 'require|regex:\w{3,12}|unique:member',
        'nickname' => 'require',
        'password' => 'require|regex:\S{6,32}',
        'password_confirm' => 'require|confirm:password',
    ];
    /**
     * 提示消息
     */
    protected $message = [
        'username.require' => '请输入用户名',
        'username.regex' => '用户名须为3-12位字符',
        'username.unique' => '用户名不能重复',
        'nickname.require' => '请输入昵称',
        'password_confirm.confirm' => '输入的两次密码不一致',
        'password_confirm.require' => '请再次输入密码',
    ];

    /**
     * 字段描述
     */
    protected $field = [
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => ['username', 'nickname', 'password', 'password_confirm'],
        'edit' => ['username', 'nickname', 'password' => 'regex:\S{6,32}', 'password_confirm' => 'confirm'],
    ];

    public function __construct(array $rules = [], $message = [], $field = [])
    {
        $this->field = [
            'username' => __('Username'),
            'nickname' => __('Nickname'),
            'password' => __('Password'),
        ];
        parent::__construct($rules, $message, $field);
    }
}